package com.iwuke.dance.career;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.imagepicker.OnResultCompressListener;
import com.iwuke.dance.util.imagepicker.PictureSelectorUtil;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;
import com.luck.picture.lib.entity.LocalMedia;
import com.yalantis.ucrop.UCrop;

import java.util.ArrayList;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/10/15
 * 添加生涯
 */
public class AddCareerActivity extends BaseActivity implements View.OnClickListener, OnResultCompressListener<LocalMedia> {

    private TitleBarView title_bar;
    private RelativeLayout rlt_type;
    private TextView tv_type;
    private EditText et_event_name;
    private EditText et_competition_project;
    private EditText et_project;
    private EditText et_issuing_authority;
    private RelativeLayout rlt_get_time;
    private TextView tv_get_time;
    private EditText et_honor_name;
    private LinearLayout rlt_proof_image;
    private TextView tv_cancel;
    private TextView tv_save;
    private LinearLayout llt_project;
    private LinearLayout llt_issuing_authority;
    private TextView tv_name_desc;
    public ArrayList<String> mImageList=new ArrayList<>();
    private ImageView img_proof_image;
    private PictureSelectorUtil pictureSelectorUtil;


    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent) {
        BaseActivity.setConnect(AddCareerActivity.this, networkEvent, title_bar);

    }

    @Override
    protected int getView() {
        return R.layout.activity_add_career;
    }

    @Override
    protected void init(View mView) {

        title_bar = findViewById(R.id.title_bar);
        //类型
        rlt_type = findViewById(R.id.rlt_type);
        //类型
        tv_type = findViewById(R.id.tv_type);
        tv_type.setText(getString(R.string.event_name));
        //证书/活动名称
        tv_name_desc = findViewById(R.id.tv_name_desc);
        et_event_name = findViewById(R.id.et_event_name);
        //参赛项目
        et_competition_project = findViewById(R.id.et_competition_project);
        //项目
        llt_project = findViewById(R.id.llt_project);
        et_project = findViewById(R.id.et_project);
        //发证单位
        llt_issuing_authority = findViewById(R.id.llt_issuing_authority);
        et_issuing_authority = findViewById(R.id.et_issuing_authority);
        //获得时间
        rlt_get_time = findViewById(R.id.rlt_get_time);
        //获得时间
        tv_get_time = findViewById(R.id.tv_get_time);
        //荣誉名称
        et_honor_name = findViewById(R.id.et_honor_name);
        //证明图片
        rlt_proof_image = findViewById(R.id.rlt_proof_image);
        //图片展示
        img_proof_image = findViewById(R.id.img_proof_image);

        //取消
        tv_cancel = findViewById(R.id.tv_cancel);
        //保存
        tv_save = findViewById(R.id.tv_save);


        title_bar.setTitleText("添加生涯");
        title_bar.setRightBoxText("生涯列表");
        title_bar.setRightBoxVisibility(true);
        title_bar.setRightBoxColor(R.color.color_666);
        title_bar.setRightBoxBackground(R.drawable.code_box_bg);
        title_bar.setLeftOnClick(this);
        title_bar.setOnRightBoxListener(this);
        //类型
        rlt_type.setOnClickListener(this);
        //获得时间
        rlt_get_time.setOnClickListener(this);
        //证明图片
        rlt_proof_image.setOnClickListener(this);
        //图片展示
        img_proof_image.setOnClickListener(this);
        //取消
        tv_cancel.setOnClickListener(this);
        //保存
        tv_save.setOnClickListener(this);

    }

    @Override
    protected void initData() {

        pictureSelectorUtil = new PictureSelectorUtil(this);
        pictureSelectorUtil.setResultCompressListener(this);

    }

    /**
     * @description 选择类型
     * @param
     * @return
     * @author 魏从响
     * @time 2025/10/15 14:02
     */
    public void setType(Context context) {

        //对于 PopupWindow，正确的做法是在 inflate 时传入 null，但需要确保在创建 PopupWindow 对象时明确指定宽度和高度。你的代码已经做到了这一点，这是关键。
        //因此，消除这个警告最直接有效的方法是在 inflate 方法上使用 @SuppressLint 注解，明确告知编译器你了解这个情况并有意为之：
        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(context).inflate(R.layout.popup_window_type, null);
        final PopupWindow popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //新方法：使用透明的 ColorDrawable   new ColorDrawable(Color.TRANSPARENT)和new BitmapDrawable()一样
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置白色背景
        //popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        //创建带圆角的背景 首先，在 res/drawable 目录下创建一个 XML 文件（如 popup_background.xml）：
        //popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.popup_background));
        //使用 NinePatch 图片作为背景 如果你的设计需要更复杂的背景效果，可以使用 .9.png 图片：
        //popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.your_nine_patch_background));
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置位置 在某个控件下面
//        popupWindow.showAsDropDown(tv_search,-10,10);
        //设置位置
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

        RelativeLayout rlt_background = view.findViewById(R.id.rlt_background);
        LinearLayout llt_content = view.findViewById(R.id.llt_content);
        TextView tv_title = view.findViewById(R.id.tv_title);//标题
        TextView tv_event_name = view.findViewById(R.id.tv_event_name);//证书
        TextView tv_activity = view.findViewById(R.id.tv_activity);//活动
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);//取消

        tv_title.setText("选择类型");
        tv_event_name.setText(getString(R.string.event_name));
        tv_activity.setText(getString(R.string.activity_name));
        tv_cancel.setText("取消");

        rlt_background.setOnClickListener((v)-> {
            popupWindow.dismiss();
        });
        llt_content.setOnClickListener((v)-> {});

        tv_event_name.setOnClickListener((v)-> {
            tv_type.setText(tv_event_name.getText().toString());
            llt_project.setVisibility(View.VISIBLE);
            llt_issuing_authority.setVisibility(View.VISIBLE);
            tv_name_desc.setText(getString(R.string.event_name_desc));
            popupWindow.dismiss();
        });

        tv_activity.setOnClickListener((v)-> {
            tv_type.setText(tv_activity.getText().toString());
            llt_project.setVisibility(View.GONE);
            llt_issuing_authority.setVisibility(View.GONE);
            tv_name_desc.setText(getString(R.string.activity_name_desc));
            popupWindow.dismiss();
        });
        tv_cancel.setOnClickListener((v)-> {
                popupWindow.dismiss();
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){//返回
            finish();
        }else if (v.getId()==R.id.tv_right_box){//生涯列表
            Util.setIntent(AddCareerActivity.this,CareerListActivity.class);
        }else if (v.getId()==R.id.rlt_type){//类型
            setType(AddCareerActivity.this);
        }else if (v.getId()==R.id.rlt_get_time){//获得时间
            Util.customDatePicker(AddCareerActivity.this,null);
            Util.getOnTime((onTime)-> {
                tv_get_time.setText(onTime);//选择的时间
            });
        }else if (v.getId()==R.id.rlt_proof_image){//证明图片
//            Util.setImagePicker(AddCareerActivity.this,mImageList);
            pictureSelectorUtil.selectImageCompressType();
        }else if (v.getId()==R.id.img_proof_image){//图片展示
//            Util.setImagePicker(AddCareerActivity.this,mImageList);
            pictureSelectorUtil.selectImageCompressType();
        }else if (v.getId()==R.id.tv_cancel){//取消
            finish();
        }else if (v.getId()==R.id.tv_save){//保存

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Util.REQUEST_SELECT_IMAGES_CODE && resultCode == RESULT_OK) {
//            mImageList = data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
//            for (int i=0;i<mImageList.size();i++){
//                if (i==0){
//                    //证明图片
//                    rlt_proof_image.setVisibility(View.GONE);
//                    //图片展示
//                    img_proof_image.setVisibility(View.VISIBLE);
//                    //小图加载
//                    Glide.with(AddCareerActivity.this).load(mImageList.get(i)).into(img_proof_image);
//                }
//            }

//            uploadImage();//上传图片

            switch (requestCode) {
                case UCrop.REQUEST_CROP:
                    final Uri imageUri = UCrop.getOutput(data);
                    //证明图片
                    rlt_proof_image.setVisibility(View.GONE);
                    //图片展示
                    img_proof_image.setVisibility(View.VISIBLE);
                    //小图加载
                    Glide.with(AddCareerActivity.this).load(imageUri).into(img_proof_image);
                    ArrayList<String> pathList=new ArrayList<>();
                    pathList.add(imageUri.getPath());
//                    try {
//                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//                        img_head.setImageBitmap(bitmap);
//                        GlideUtil.setImageViewRadius(PersonalSettingsActivity.this, bitmap, 30, img_head);
//                        LogUtil.logD("++++++++++++++bitmap=", imageUri.getPath());

//                    } catch (FileNotFoundException e) {
//                        e.printStackTrace();
//                    }
                    break;
            }
        }
    }


    @Override
    public void onResult(ArrayList<LocalMedia> result) {
        //证明图片
        rlt_proof_image.setVisibility(View.GONE);
        //图片展示
        img_proof_image.setVisibility(View.VISIBLE);
        ArrayList<String> pathList=new ArrayList<>();
        pathList.clear();
        pathList.add(result.get(0).getPath());
        for (int i=0;i<result.size();i++){
            pathList.add(result.get(i).getPath());
        }
        //小图加载
        if (img_proof_image!=null)
            Glide.with(AddCareerActivity.this).load(result.get(0).getPath()).into(img_proof_image);
    }

    @Override
    public void onCancel() {

    }
}
